package com.xyh.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyh.eduservice.entity.EduChapter;
import com.xyh.eduservice.entity.EduVideo;
import com.xyh.eduservice.entity.chapter.ChapterVo;
import com.xyh.eduservice.entity.chapter.VideoVo;
import com.xyh.eduservice.mapper.EduChapterMapper;
import com.xyh.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyh.eduservice.service.EduVideoService;
import com.xyh.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-08
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Resource
    private EduVideoService videoService;
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //根据课程id查出所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> chapters = baseMapper.selectList(wrapperChapter);
        //根据课程id 查出所有小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> videos = videoService.list(wrapperVideo);

        List<ChapterVo> chapterVoList = new ArrayList<>();
        for (EduChapter chapter :chapters) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            chapterVoList.add(chapterVo);

            List<VideoVo> videoVoList = new ArrayList<>();
            for (EduVideo video :videos) {
                if (!chapter.getId().equals(video.getChapterId())) continue;
                VideoVo videoVo = new VideoVo();
                BeanUtils.copyProperties(video,videoVo);
                videoVoList.add(videoVo);
            }
            if (videoVoList.isEmpty()) continue;
            chapterVo.setChildren(videoVoList);
        }

        return chapterVoList;
    }

    //删除章节的查询
    @Override
    public boolean deleteChapter(String chapterId) {
        //先查询章节下面是否有小节，有小节就不能删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        if (count > 0){    //有小节,不删除
             throw new GuliException(20001,"有小节，无法删除");
        }else {  //没小节，删除
            int delete = baseMapper.deleteById(chapterId);
            return delete > 0;
        }

    }

    //根据课程 id 删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
